import { Component, OnInit, ViewChild } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { Router, ActivatedRoute } from '@angular/router';
import { NzModalService } from 'ng-zorro-antd/modal';
import { SellPromotionManageService } from '../sell-promotion-manage.service';
import { PermissionService } from 'src/app/service/permission.service';

@Component({
  selector: 'app-direct-descent-details',
  templateUrl: './direct-descent-details.component.html',
  styleUrls: ['./direct-descent-details.component.scss']
})
export class DirectDescentDetailsComponent implements OnInit {
  @ViewChild('myFollowup') followupModel: any;

  routerParams: any = {};
  // 详情data
  detailInfoData: any = {};
  // 订单状态
  orderStateOptions: any = {
    0: '有效',
    1: '无效',
    2: '待付款',
    3: '待审核',
    4: '审核不通过',
    5: '审核通过',
    6: '排队发货',
    7: '待发货',
    8: '待收货(已发货)',
    9: '交易完成',
    10: '支付超时',
    11: '客服取消',
    12: '客户取消',
    13: '售后中',
    14: '全部退货'
  };

  // table
  tableForms: any = {
    status: '',
    tableLoading: false,
    page: 1,
    pageSize: 20,
    total: 0
  };
  // 表格data
  tableData: any = [];

  // 跟进
  oddNumber: any = ''; // 关联信息(订单号 售后单号 客户id 出库单号)

  constructor(
    private message: NzMessageService,
    private activatedRoute: ActivatedRoute,
    private modal: NzModalService,
    private services: SellPromotionManageService,
    public permission: PermissionService
  ) {
    this.activatedRoute.params.subscribe((res: any) => {
      this.routerParams = res || {};
    })
  }

  ngOnInit(): void {
    this.loadDetailsData();
    this.loadTable();
  }

  async loadDetailsData() {
    try {
      let params = {
        id: this.routerParams.id,
        page: 1,
        pageSize: 1
      }
      const res: any = await this.services.queryPromotionDetailsData(params);
      if (res.code == 0) {
        const { promotionInfo } = res.data || {};
        this.detailInfoData = promotionInfo || {};
        this.detailInfoData.discountTable = promotionInfo.discountContent ? JSON.parse(promotionInfo.discountContent) : [];
      } else {
        this.message.error(res.message || '详情获取失败');
      }
    } catch (error: any) {
      this.message.error(error);
      this.detailInfoData = {};
    }
  }

  // 记录table
  async loadTable() {
    try {
      let params = {
        id: this.routerParams.id,
        status: this.tableForms.status,
        page: this.tableForms.page,
        pageSize: this.tableForms.pageSize
      }

      this.tableForms.tableLoading = true;
      const res: any = await this.services.queryPromotionDetailsData(params);
      this.tableForms.tableLoading = false;
      if (res.code == 0) {
        const { records, total }: any = res.data?.list || {};
        this.tableData = records || [];
        this.tableForms.total = total || 0;
      } else {
        this.message.error(res.message || '使用记录获取失败');
      }
    } catch (error: any) {
      this.tableForms.tableLoading = false;
      this.tableData = [];
      this.tableForms.total = 0;
      this.message.error(error);
    }
  }

  // 订单状态change
  orderStateChange(reslut: any) {
    this.tableForms.status = reslut || '';
    this.tableForms.page = 1;
    this.loadTable();
  }

  // open model
  openModel(item: any) {
    this.oddNumber = item.orderNum;
    this.followupModel.showModal();
  }
  // submit
  submitData(data: any) {
    // console.log(data);
    this.loadTable();
  }

  // 取消订单
  cannlOrder(item: any) {
    this.modal.confirm({
      nzTitle: '取消订单',
      nzContent: '<b style="color: red;">确认取消？该操作不可逆，请提前和用户沟通</b>',
      nzOkText: '确定',
      nzOkType: 'primary',
      nzOkDanger: true,
      nzOnOk: async () => {
        try {
          this.message.loading('loading...', {nzDuration: 0});
          const res: any = await this.services.cancelOrderData({ orderId: item.orderId });
          this.message.remove();
          if (res.code == 0) {
            this.message.success('订单取消成功');
            this.loadTable();
          } else {
            this.message.error(res.message || '订单取消失败');
          }
        } catch (error: any) {
          this.message.remove();
          this.message.error(error);
        }
      },
      nzCancelText: '取消',
      nzOnCancel: () => console.log('Cancel')
    });
  }

  // 当前页码改变时的回调函数
  onPageIndexChange(index: number) {
    this.tableForms.page = index;
    this.loadTable();
  }
  // 页数改变时回调
  onPageSizeChange(sizeNum: number) {
    this.tableForms.page = 1;
    this.tableForms.pageSize = sizeNum;

    this.loadTable();
  }
}
